﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using FTools.Dominio;

namespace FTools.Persistencia
{
    class PerPedidoReng
    {
        static string msg = "";

        //OBTENER COLECCION PedidoReng (Por Pedido)
        public ArrayList obtenerColeccionPedidoReng(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdPedido)
        {
            SqlDataAdapter dataAdapter;
            DataTable dataTable = new DataTable();
            ArrayList colPedidoReng = new ArrayList();
            PerSubProducto objPerSubProducto = new PerSubProducto();

            try
            {
                SqlCommand command = new SqlCommand("select * from PEDIDO_RENG where ID_PEDIDO=@IdPedido");

                command.Parameters.AddWithValue("@IdPedido", xIdPedido);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    DataRow fila = dataTable.Rows[i];
                    PedidoReng objPedidoReng = new PedidoReng();
                    SubProducto objSubProducto = objPerSubProducto.obtenerSubProducto(xConexion, xTransaccion, (long)fila.ItemArray[1]);
                    objPedidoReng.IdPedido = (long)fila.ItemArray[0];
                    objPedidoReng.ObjSubProducto = objSubProducto;
                    objPedidoReng.Cantidad = (long)fila.ItemArray[2];

                    colPedidoReng.Add(objPedidoReng);
                }
                return colPedidoReng;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE PedidoReng
        public Boolean existePedidoReng(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdPedido, long xIdSubProducto)
        {
            DataTable dataTable = new DataTable();

            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from PEDIDO_RENG where ID_PEDIDO=@IdPedido and ID_SUB_PRODUCTO=@IdSubProducto");

                command.Parameters.AddWithValue("@IdPedido", xIdPedido);
                command.Parameters.AddWithValue("@IdSubProducto", xIdSubProducto);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //ALTA RENG_PEDIDO
        public int altaPedidoReng(SqlConnection xConexion, SqlTransaction xTransaccion, PedidoReng xObjPedidoReng)
        {
            try
            {
                SqlCommand command = new SqlCommand("insert into PEDIDO_RENG values (@IdPedido, @IdSubProducto, @Cantidad)");

                command.Parameters.AddWithValue("@IdPedido", xObjPedidoReng.IdPedido);
                command.Parameters.AddWithValue("@IdSubProducto", xObjPedidoReng.ObjSubProducto.IdSubProducto);
                command.Parameters.AddWithValue("@Cantidad", xObjPedidoReng.Cantidad);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //BAJA RENG_PEDIDO (Por Renglon)
        public int bajaPedidoReng(SqlConnection xConexion, SqlTransaction xTransaccion, PedidoReng xObjPedidoReng)
        {
            try
            {
                SqlCommand command = new SqlCommand("delete from PEDIDO_RENG where ID_PEDIDO=@IdPedido and ID_SUB_PRODUCTO=@IdSubProducto");

                command.Parameters.AddWithValue("@IdPedido", xObjPedidoReng.IdPedido);
                command.Parameters.AddWithValue("@IdSubProducto", xObjPedidoReng.ObjSubProducto.IdSubProducto);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //BAJA RENG_PEDIDO (Por Pedido)
        public int bajaPedidoReng(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdPedido)
        {
            try
            {
                SqlCommand command = new SqlCommand("delete from PEDIDO_RENG where ID_PEDIDO=@IdPedido");

                command.Parameters.AddWithValue("@IdPedido", xIdPedido);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //MODIFICA RENG_PEDIDO
        public int modificaPedidoReng(SqlConnection xConexion, SqlTransaction xTransaccion, PedidoReng xObjPedidoReng)
        {
            try
            {
                SqlCommand command = new SqlCommand("update PEDIDO_RENG set CANTIDAD=@Cantidad " +
                                                    "where ID_PEDIDO=@IdPedido and ID_SUB_PRODUCTO=@IdSubProducto");

                command.Parameters.AddWithValue("@IdPedido", xObjPedidoReng.IdPedido);
                command.Parameters.AddWithValue("@IdSubProducto", xObjPedidoReng.ObjSubProducto.IdSubProducto);
                command.Parameters.AddWithValue("@Cantidad", xObjPedidoReng.Cantidad);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }
    }
}
